package nl.saxion.ein4b;

import java.util.ArrayList;

import nl.saxion.ein4b.controller.Blackjack;
import nl.saxion.ein4b.model.Card;
import nl.saxion.ein4b.model.Profile;
import nl.saxion.ein4b.model.ProfileManager;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

public class GameActivity extends Activity {

	private Button bet5, bet10, bet25, bet50, bet100;
	private Button hit, stand, doubleDown, refill, deal;
	private GameView gv;
	private Blackjack blackjack;
	private ProfileManager m;

	private View totalview;

	private int prevCredit = 0;

	private ArrayList<Integer> coins = new ArrayList<Integer>();

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.game);

		Profile profile;

		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			profile = (Profile) extras.getSerializable("profile");
		} else
			profile = new Profile("Speler", "", 100, 0);

		m = new ProfileManager(this.getBaseContext());

		blackjack = new Blackjack(profile);

		bet5 = (Button) findViewById(R.id.id_bet5);
		bet10 = (Button) findViewById(R.id.id_bet10);
		bet25 = (Button) findViewById(R.id.id_bet25);
		bet50 = (Button) findViewById(R.id.id_bet50);
		bet100 = (Button) findViewById(R.id.id_bet100);
		hit = (Button) findViewById(R.id.button1);
		stand = (Button) findViewById(R.id.button2);
		doubleDown = (Button) findViewById(R.id.button5);
		refill = (Button) findViewById(R.id.refillid);
		deal = (Button) findViewById(R.id.button3);

		gv = new GameView(this);

		totalview = findViewById(R.id.linearLayoutgame);
		totalview.setOnTouchListener(new GameTouchListener());

		ViewGroup gameframe = (ViewGroup) findViewById(R.id.gameViewFrame);
		gameframe.addView(gv);

		prevCredit = blackjack.getProfile().getCredit();

		drawField();
	}

	private boolean newGame = false;

	private class GameTouchListener implements OnTouchListener {

		public boolean onTouch(View v, MotionEvent event) {

			if (newGame) {
				drawField();
				newGame = false;
			}
			return true;
		}

	}

	public void drawField() {

		// Check all the buttons
		if (blackjack.hasLost() || blackjack.hasWon()) {

			int diffCredit = Math.abs(blackjack.getProfile().getCredit()
					- prevCredit);

			if (blackjack.hasWon()) {
				Toast.makeText(getApplicationContext(),
						"You won " + diffCredit + " credits", Toast.LENGTH_LONG)
						.show();
			} else {
				Toast.makeText(getApplicationContext(),
						"You lost " + diffCredit + " credits",
						Toast.LENGTH_LONG).show();
			}

			newGame = true;
		}

		// clear gameView
		gv.clear();

		checkIfVisible(deal, blackjack.canDeal());
		checkIfVisible(hit, blackjack.canHit());
		checkIfVisible(stand, blackjack.canStand());
		checkIfVisible(doubleDown, blackjack.canDoubleDown());
		checkIfVisible(refill, blackjack.canRefill());
		checkIfVisible(bet5, blackjack.canBet(5));
		checkIfVisible(bet10, blackjack.canBet(10));
		checkIfVisible(bet25, blackjack.canBet(25));
		checkIfVisible(bet50, blackjack.canBet(50));
		checkIfVisible(bet100, blackjack.canBet(100));

		int i = 0;
		// draw player cards + value of cards
		for (Card card : blackjack.getPlayerStack().getCardStack()) {
			gv.drawCard(card, 140 + i, 450);
			i += 13;
		}
		if (blackjack.getPlayerHandValue() > 0)
			gv.drawText(Integer.toString(blackjack.getPlayerHandValue()), 80,
					530);

		// draw Dealer cards + value of cards
		i = 0;
		for (Card Dcard : blackjack.getDealerStack().getCardStack()) {
			gv.drawCard(Dcard, 140 + i, 100);
			i += 13;
		}
		if (blackjack.getDealerHandValue() > 0)
			gv.drawText(Integer.toString(blackjack.getDealerHandValue()), 80,
					180);

		// draw cardstack
		if (blackjack.getMainStack() != null) {
			int cardstacksize = blackjack.getMainStack().getCardStack().size();
			for (int h = 0; h < cardstacksize; h++) {
				gv.drawCard(null, 350, 150 - h * 2);
			}
		}

		// draw bet value
		gv.drawText(Integer.toString(blackjack.getPlayer().getBetValue()), 50,
				450);

		// draw coins
		drawBet();

		// draw credits
		gv.drawText(blackjack.getProfile().getName() + "s credit: "
				+ blackjack.getProfile().getCredit() + " ", 50, 600);

		if (newGame) {
			Profile p = blackjack.getProfile();
			coins.clear();
			blackjack = new Blackjack(p);
			prevCredit = p.getCredit();
		}

		m.saveProfile(blackjack.getProfile());
	}

	private void checkIfVisible(Button button, boolean visible) {
		if (!visible || blackjack.hasLost() || blackjack.hasWon()) {
			button.setClickable(false);
			button.setVisibility(Button.INVISIBLE);
		} else {
			button.setClickable(true);
			button.setVisibility(Button.VISIBLE);
		}
	}

	private void drawBet() {
		int i = 15;
		// draw 5 coins
		drawCoinStack(50 + i, 350, 5);
		// draw 10 coins
		drawCoinStack(125 + i, 350, 10);
		// draw 25 coins
		drawCoinStack(200 + i, 350, 25);
		// draw 50 coins
		drawCoinStack(275 + i, 350, 50);
		// draw 100 coins
		drawCoinStack(350 + i, 350, 100);

	}

	private void drawCoinStack(int x, int y, int value) {
		int i = 0;
		gv.drawOval(x - 5, y - 5, 60, 60);
		for (int c : coins) {
			if (c == value) {
				i += 2;
				gv.drawCoin(c, x, y - i);// (coinvalue,x,y)
			}
		}
	}

	public void hit(View v) {
		blackjack.hit(blackjack.getPlayerStack());
		drawField();
	}

	private void bet(int amount) {
		coins.add(amount);
		drawField();
	}

	public void stand(View v) {
		blackjack.stand();
		drawField();
	}

	public void refill(View v) {
		blackjack.refill();
		drawField();
	}

	public void bet5(View v) {
		blackjack.bet(5);
		bet(5);
		drawField();
	}

	public void bet10(View v) {
		blackjack.bet(10);
		bet(10);
		drawField();
	}

	public void bet25(View v) {
		blackjack.bet(25);
		bet(25);
		drawField();
	}

	public void bet50(View v) {
		blackjack.bet(50);
		bet(50);
		drawField();
	}

	public void bet100(View v) {
		blackjack.bet(100);
		bet(100);
		drawField();
	}

	public void deal(View v) {
		blackjack.setup();
		drawField();
	}

	public void doubleDown(View v) {
		blackjack.doubleDown();
		drawField();
	}
}
